<template>
	<view class="home-wrap u-m-b-20"  v-cloak>
		<base-nav-bar :backIconColor="'#333'" :isBack="true" :background="{'background':'#fff'}">
			<text
				slot="content"
				style="color: #333; width: 100%; text-align: center;padding-left: 20rpx;"
			>
				{{ teacherId?'编辑老师':'新增老师' }}
			</text>
		</base-nav-bar>
		<view class="content-box u-p-20 u-m-t-30 u-m-l-40 u-m-r-40">
			<!-- grid商家功能模块 -->
			<view class="u-flex u-flex-items-center u-flex-start u-flex-wrap">
				<u--form :model="form" ref="uForm" :labelPosition="'top'" style="width: 100%;" :labelStyle="{'margin-botttom':'10rpx'}">
					<u-form-item label="老师名称" prop="name" labelWidth="120">
						<u-input v-model="form.name" placeholder="请输入老师名称"/>
					</u-form-item>
					<u-form-item label="老师描述" prop="description" labelWidth="120">
						<u--textarea v-model="form.description" placeholder="请输入老师介绍" ></u--textarea>
					</u-form-item>
					<u-form-item label="老师手机号" prop="phone" labelWidth="120">
						<u-input v-model="form.phone" placeholder="请输入老师手机号"/>
					</u-form-item>
					<u-form-item label="请上传老师图片" prop="name" labelWidth="120">
						<u-upload
							:fileList="teacherImages"
							@afterRead="afterRead"
							@delete="deletePic"
							name="Images"
							multiple
							:maxCount="1"
							:previewFullImage="true"
						></u-upload>
					</u-form-item>
				</u--form>
				<view class="submit-btn" @click="submit">提交</view>
			</view>
		</view>
	</view>
</template>

<script>

import { mapGetters } from 'vuex';
import { HTTP_IMG_URL } from '../../config/app.js'
import BaseNavBar from "../../components/BaseNavBar.vue";

export default {
	components: {
		BaseNavBar
	},
	data() {
		return {
			teacherImages:[],
			form: {
				name: '',
				description:'',
				phone:'',
				image:'',
			},
			teacherId:'',
			teacherList:[],
			rules: {
				name: [
					{
						required: true,
						message: '请输入名称',
						trigger: ['blur', 'change']
					}
				],
				phone: [
					{
						required: true,
						message: '请输入手机号',
						trigger: ['blur', 'change']
					},
					{
						// 自定义验证函数，见上说明
						validator: (rule, value, callback) => {
							// 上面有说，返回true表示校验通过，返回false表示不通过
							// uni.$u.test.mobile()就是返回true或者false的
							return uni.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						// 触发器可以同时用blur和change
						trigger: ['change','blur'],
					}
				],
			},
		};
	},
	computed: {
		...mapGetters(['isLogin','userInfo']),
		
	},
	onPullDownRefresh() {
	
	},
	
	onShow() {
	},
	onHide() {
	},
	onLoad(options) {
		console.log("====options",options)
		if(options&&options.id){
			this.teacherId = options.id
			this.getTeacherDetails()
		}
	},
	onReady() {
		//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		this.$refs.uForm.setRules(this.rules)
	},
	methods: {
		HTTP_IMG_URL() {
			return HTTP_IMG_URL
		},
		// 路由跳转
		jump(path, parmas) {
			uni.navigateTo({
				url: path,
				query: parmas
			});
		},
		submit() {
			let imageArr = [];
			if(this.teacherImages.length>0){
				this.teacherImages.forEach(item=>{
					imageArr.push(item.url)
				})
			}
			this.$refs.uForm.validate().then(res => {
				let fromData = {
					...this.form,
					image:imageArr.join(','),
				}
				if(this.teacherId){
					fromData = {
						...fromData,
						id:this.teacherId
					}
					this.$Api.merchant.merchantTeacherEditPost(fromData).then(res=>{
						uni.$u.toast(this.teacherId?'修改成功':'新增成功')
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
						},500)
					}).catch(err=>{
						console.log(e)
					})
				}else{
					this.$Api.merchant.merchantTeacherAdd(fromData).then(res=>{
						uni.$u.toast(this.teacherId?'修改成功':'新增成功')
						setTimeout(()=>{
							uni.navigateBack({
								delta: 1
							})
						},500)
					}).catch(err=>{
						console.log(e)
					})
				}
				
			}).catch(errors => {
				uni.$u.toast('校验失败')
			})
		},
		// 删除图片
		deletePic(event) {
			console.log("===111",`${event.index}`)
			this[`teacher${event.name}`].splice(event.index, 1)
			console.log("===111",this[`teacher${event.name}`])
		},
		// 新增图片
		async afterRead(event) {
			// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
			let lists = [].concat(event.file)
			let fileListLen = this[`teacher${event.name}`].length
			for (let i = 0; i < lists.length; i++) {
				const result = await this.$uploadFilePromise(lists[i].url)
				this[`teacher${event.name}`].splice(fileListLen, 1, {
					status: 'success',
					message: '',
					url: result.fullurl
				})
				fileListLen++
			}
		},
		getTeacherDetails(){
			var that = this;
			that.$Api.merchant.merchantTeacherEdit({id:this.teacherId}).then(res=>{
				this.form = res.data;
				this.teacherImages = [];
				let imageArr = this.form.image.split(',');
				imageArr.forEach(item=>{
					let obj = {
						url: item
					}
					this.teacherImages.push(obj)
				})
			}).catch(err=>{
				console.log(e)
			})
		}
	}
};
</script>

<style lang="scss" scoped>
[v-cloak] {
	display: none;
}
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
.home-wrap {
	min-height: 100vh;
	background: #F6F6F8;
	position: relative;
}
.kehu-item {
	border-radius: 20rpx;
	width: 100%;
	.swipers{
		width: 100%;
	}
}
.nickname{
	font-size: 34rpx;
	font-weight: bold;
}
.content-box{
	background: #FFFFFF;
	border-radius: 30rpx;
	min-height: 80vh;
	.pay-item{
		width: 25%;
	}
}
::v-deep .u-form{
	width: 100%;
}
.images-content{
	width: 100%;
	height: 300rpx;
	display: flex;
	border:1rpx solid #eee;
}
.submit-btn{
	margin-top: 20rpx;
	width: 100%;
	height: 84rpx;
	background: linear-gradient( #30AA4D 0%, #20CE83 100%);
	box-shadow: 0px 2px 4px 0px rgba(48,170,77,0.1);
	border-radius: 8px;
	display: flex;
	justify-content: center;
	align-items: center;
	color: #fff;
	font-size: 28rpx;
}
.u-page__tag-item {
	margin-right: 20px;
}
::v-deep .u-tag--success--plain{
	background: #F6F6F8 !important;
	border-color: #EDEDED !important;
	color: #C7C7C7 !important;
}
::v-deep .u-tag__text--success--plain{
	color: #C7C7C7 !important;
}
::v-deep .u-tag--success{
	background: linear-gradient(#30AA4D 0%, #20CE83 100%);
	box-shadow: 0px 2px 4px 0px rgba(48,170,77,0.1);
	border-width: 1px;
	border-color: #30AA4D;
}
.teShu-item{
	::v-deep .u-form-item__body{
		flex-direction: row!important;
	}
}
</style>
